package api_day09;

import org.junit.Test;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;

public class TestDemo {
    public static void main(String[] args) throws UnsupportedEncodingException {
        //编码解码测试
        String hi = "你好";   //默认设置使用utf-8编码集
        byte[] bytes = hi.getBytes("gbk");  //转换成按gbk编码的字节码数组
        String s = new String(bytes,"gbk");   //然后再解码得到gbk编码的字符串
        byte[] bytes1 = s.getBytes("utf-8");
        String s1 = new String(bytes1,"gbk");
        System.out.println(Arrays.toString(bytes1));
        System.out.println(s1);

    }
    @Test
    public void test(){
        String str = "1+2+3";
        //result用于存储计算结果
        int result = 0;
        //利用正则表达式匹配输入
        String num = "\\d",opera = "[+\\-*/]";
        String[] number = str.split(opera);
        String[] operation = str.split(num);
        System.out.println(Arrays.toString(number));
        System.out.println(Arrays.toString(operation));
        switch (operation[1]){
            case "+":
                result = Integer.parseInt(number[0]) + Integer.parseInt(number[1]);
                break;
            case "-":
                result = Integer.parseInt(number[0]) - Integer.parseInt(number[1]);
                break;
            case "*":
                result = Integer.parseInt(number[0]) * Integer.parseInt(number[1]);
                break;
            case "/":
                result = Integer.parseInt(number[0]) / Integer.parseInt(number[1]);
                break;
        }
        System.out.println(result);
    }
}
